package com.wondersgroup.fjzlpt.org.controller.view.jgqy.qysq;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.quick.framework.util.QuickPrincipalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.utils.RequestUtils;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.jgqy.qysq.QysqKsxxDAO;
import com.wondersgroup.fjzlpt.org.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.org.service.inter.jgqy.qysq.QysqService;

@Controller
@RequestMapping("qysq")
public class QysqController {
	
	@Autowired
	private YljgxxDAO yljgxxDAO;
	
	@Autowired
	private QysqDAO qysqDAO;
	
	@Autowired
	private QysqService qysqService;
	
	@Autowired
	private QysqKsxxDAO qysqKsxxDAO;
	
	@RequestMapping("/index.html")
	public String index(HttpServletRequest request)
	{
        JSONObject json = new JSONObject();
        json.put("PTJGID", QuickPrincipalUtils.getPtjgID(request));
        request.setAttribute("userInfo", json);
		return "jgqy/qysq/index";
	}
	
	@RequestMapping("/manager.html")
	public String manager(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		/**判断从新增还是修改方式进入*/
		if(map.containsKey("YLTID")){
			/**获取医联体信息*/
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**获取签约机构信息*/
			map.put("YLTLX", jo.getString("YLTLX"));
			JSONArray ja = qysqService.queryQyJg(map);
			request.setAttribute("qyjgxx", ja);
			/**获取主导机构信息*/
			map.put("YLJGID", jo.getString("ZDYLJGID"));
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = yljgxxDAO.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			/**专科联盟下，还需获取主导机构的科室信息*/
			if("03".equals(map.get("YLTLX"))){
				json.putAll(qysqService.querZdjgKsxx(map));
			}
			request.setAttribute("fqyljgxx", json);
		}
		if(map.containsKey("YLJGID")){
			/**获取主导机构信息*/ 
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject jo = yljgxxDAO.queryPage(map);
			request.setAttribute("fqyljgxx", jo.get("rows"));
		}
		return "jgqy/qysq/manager";
	}
	
	@RequestMapping("/detail.html")
	public String detail(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		if(map.containsKey("YLTID")){
			/**获取医联体信息*/
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			String yljgid = map.remove("YLJGID");
			JSONObject jo = qysqDAO.queryYltxx(map);
			jo = (JSONObject) (jo.getJSONArray("rows")).get(0);
			request.setAttribute("qysqxx", jo);
			/**获取签约机构信息*/
			map.put("YLTLX", jo.getString("YLTLX"));
			JSONArray ja = qysqService.queryQyJg(map);
			request.setAttribute("qyjgxx", ja);
			/**获取主导机构信息*/
			if(!StringUtils.isEmpty(yljgid)){
				map.put("YLJGID", yljgid);
			}else{
				map.put("YLJGID", jo.getString("ZDYLJGID"));
			}
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = yljgxxDAO.queryPage(map);
			json = (JSONObject) (json.getJSONArray("rows")).get(0);
			/**专科联盟下，还需获取主导机构的科室信息*/
			if("03".equals(map.get("YLTLX"))){
				json.putAll(qysqService.querZdjgKsxx(map));
			}
			request.setAttribute("fqyljgxx", json);
		}
		return "jgqy/qysq/detail";
	}
	
	@RequestMapping("/yljgxq.html")
	public String yljgxq(HttpServletRequest request)
	{
		Map<String, String> map = RequestUtils.GetParameterMap(request);
		if(map.containsKey("YLJGID")){
			map.put("pageSize", "1");
			map.put("pageNumber", "1");
			JSONObject json = yljgxxDAO.queryPage(map);
			request.setAttribute("yljgxx", json.get("rows"));
		}
		return "jgqy/qysq/yljg-detail";
	}
	
	@RequestMapping("/yljg.html")
	public String yljg(HttpServletRequest request)
	{
		return "jgqy/qysq/yljg";
	}
	
	
	@RequestMapping("/kslb.html")
	public String kslb(HttpServletRequest request)
	{
		return "jgqy/qysq/kslb";
	}
	
	/**
	 * 新建医联体-选择协议
	 * @param request
	 * @return
	 */
	@RequestMapping("/managerXzxy.html")
	public String managerXzxy(HttpServletRequest request)
	{
		return "jgqy/qysq/manager_xzxy";
	}
	
	/**
	 * 新建医联体-选择协议
	 * @param request
	 * @return
	 */
	@RequestMapping("/managerBjxy.html")
	public String managerBjxy(HttpServletRequest request)
	{
		return "jgqy/qysq/manager_bjxy";
	}
	
	/**
	 * 新建医联体-查看协议
	 * @param request
	 * @return
	 */
	@RequestMapping("/ckxy.html")
	public String ckxy(HttpServletRequest request)
	{
		return "jgqy/qysq/ckxy";
	}
}
